package com.pw.study.server.tcp;

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

import static org.apache.xmlbeans.impl.schema.StscState.start;

/**
 * @Author: linux_future
 * @since: 2022/4/2
 **/
public class ClientV1 {

    private Socket socket;
    private InputStream in;
    private InputStreamReader input;
    private OutputStream out;

    public static void main(String[] args) throws IOException {
        new ClientV1().startClient(6666);
    }

    private void startClient(int port) {
        System.out.println("客户端1启动。。。。。。");

        try {
            socket = new Socket("localhost", 6666);
            new Thread() {

                @Override
                public void run() {
                    System.out.println("running..............");
                    while (true) {
                        System.out.println("run while.............");
                        try {
                            in = socket.getInputStream();
                            BufferedReader stream =new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8)) ;
                            System.out.println("客户端===" +stream.readLine() );
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }

                }
            }.start();
            //输入数据
            out = socket.getOutputStream();
            BufferedWriter writerClient = new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8));
            while (true) {
                System.out.print("client开始输入数据：");
                input = new InputStreamReader(System.in);
                String line = new BufferedReader(input).readLine();
                writerClient.write("客户端v1:" + line);
                writerClient.flush();
                System.out.println("发送消息。。。。。。。。。");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                in.close();
                out.close();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}
